/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */
import chalk from 'chalk';
import path from 'path';
import fs from 'fs';
import findPodfilePath from './findPodfilePath';
import findXcodeProject from './findXcodeProject';
import findPodspec from './findPodspec';
import findAllPodfilePaths from './findAllPodfilePaths';
import {
  IOSProjectParams,
  IOSDependencyParams,
  IOSProjectConfig,
  IOSDependencyConfig,
} from '@react-native-community/cli-types';
import {CLIError} from '@react-native-community/cli-tools';
import {BuilderCommand} from '../types';

/**
 * Returns project config by analyzing given folder and applying some user defaults
 * when constructing final object
 */
export const getProjectConfig =
  ({platformName}: BuilderCommand) =>
  (folder: string, userConfig: IOSProjectParams): IOSProjectConfig | null => {
    if (!userConfig) {
      return null;
    }

    const src = path.join(folder, userConfig.sourceDir ?? '');
    const podfile = findPodfilePath(src, platformName);

    /**
     * In certain repos, the Xcode project can be generated by a tool.
     * The only file that we can assume to exist on disk is `Podfile`.
     */
    if (!podfile) {
      return null;
    }

    const sourceDir = path.dirname(podfile);

    const xcodeProject = findXcodeProject(fs.readdirSync(sourceDir));

    return {
      sourceDir,
      watchModeCommandParams: userConfig.watchModeCommandParams,
      xcodeProject,
      automaticPodsInstallation: userConfig.automaticPodsInstallation,
      assets: userConfig.assets ?? [],
    };
  };

/**
 * Make getDependencyConfig follow the same pattern as getProjectConfig
 */
export const getDependencyConfig =
  ({}: BuilderCommand) =>
  (
    folder: string,
    userConfig: IOSDependencyParams | null = {},
  ): IOSDependencyConfig | null => {
    if (userConfig === null) {
      return null;
    }

    const podspecPath = findPodspec(folder);

    if (!podspecPath) {
      return null;
    }

    let version = 'unresolved';

    try {
      const packageJson = require(path.join(folder, 'package.json'));

      if (packageJson.version) {
        version = packageJson.version;
      }
    } catch {
      throw new CLIError(
        `Failed to locate package.json file from ${chalk.underline(
          folder,
        )}. This is most likely issue with your node_modules folder being corrupted. Please force install dependencies and try again`,
      );
    }

    return {
      podspecPath,
      version,
      configurations: userConfig.configurations || [],
      scriptPhases: userConfig.scriptPhases || [],
    };
  };

export const findPodfilePaths = findAllPodfilePaths;
